// const App = {
//   data() {
//     return {
//       personal_information: {
//         ID_Card_Number: "",
//         Personal_Name: "",
//         Birthday: "",
//         Gender: "", //性别
//         Registered_Residence: "", //户籍所在
//         Nation: "",
//         Political_Affiliation: "",
//         RR_Province_Number: "",
//         RR_City_Number: "",
//         RR_County_Nubmer: "",
//         RR_Town_Number: "",
//         Home_Address: "",
//       },
//       Genderdata: [
//         {
//           value: "男",
//           label: "男",
//         },
//         {
//           value: "女",
//           label: "女",
//         },
//       ],

//       value: [], // 初始化 labelNumber 属性
//       hjoptions: [], //省市区县
//       Nation: [], //民族
//       fileList: [], //身份证照片
//     };
//   },
//   mounted() {
//     let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
//     console.log(TABLE_NAME);
//     this.Data = TABLE_NAME;
//     let type = sessionStorage.getItem("type");
//     console.log(type);
//     if (type == "Edit") {
//       let key = sessionStorage.getItem("key");
//       console.log(key);
//       this.Edit(key);
//     }
//     this.getNation();
//     this.cascaderChange();
//   },
//   methods: {
//     //获取56个民族
//     getNation() {
//       const _this = this;
//       $.ajax({
//         //文件路径
//         url: "../../../api/Nation.json",
//         type: "GET",
//         success: function (data) {
//           console.log(data);
//           _this.Nation = data;
//         },
//         dataType: "json",
//       });
//     },
//     // 级联方法
//     cascaderChange() {
//       const _this = this;
//       $.ajax({
//         url: "../../../api/Districtjl.json",
//         type: "GET",
//         success: function (data) {
//           console.log(data);
//           _this.hjoptions = data;
//         },
//         dataType: "json",
//       });
//     },
//     //级联的西选择事件
//     hlhandleChange(value) {
//       console.log("el-cascader 选择的值:", value);
//       this.personal_information.RR_Province_Number = value[0];
//       this.personal_information.RR_City_Number = value[1];
//       this.personal_information.RR_County_Nubmer = value[2];
//       this.RR_Town_Number = value[3];
//       console.log(this.value);
//     },
//     //编辑进来方法
//     Edit(key) {
//       let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
//       console.log(TABLE_NAME);
//       let rowkey = sessionStorage.getItem("rowkey");
//       console.log(rowkey);
//       const _this = this;
//       $.ajax({
//         headers: {
//           "content-type": "application/x-www-form-urlencoded",
//         },
//         url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
//         method: "post",
//         data: {
//           Data: TABLE_NAME, //表名
//           Condition: `${key}='${rowkey}'`,
//         },
//         success: (res) => {
//           console.log(res);
//           _this.personal_information = res[0];
//           _this.value = [
//             res[0].RR_Province_Number,
//             res[0].RR_City_Number,
//             res[0].RR_County_Nubmer,
//             res[0].RR_Town_Number,
//           ];
//         },
//         error: (err) => {
//           console.log(err);
//         },
//       });
//     },
//     //提交表单
//     handleSubmit() {
//       let type = sessionStorage.getItem("type");
//       console.log(type);
//       const appElement = document.getElementById("MainTable");
//       console.log(appElement);
//       let dataOptions = appElement.getAttribute("data-options");
//       console.log(dataOptions);
//       //动态获取到的表单数据
//       let options = JSON.parse(dataOptions);
//       console.log("options", options);
//       let lArray = new Array();
//       lArray.push(this[options.form]);
//       //动态获取的表名
//       let bm = options.TableName;
//       console.log(bm);
//       let lJSONObject = new Object();
//       lJSONObject[bm] = lArray; //换表
//       let lJSONObject_DataSet = new Object(); // Modify
//       if (type == "Edit") {
//         lJSONObject_DataSet.Modify = lJSONObject; //换表
//       } else {
//         lJSONObject_DataSet.Add = lJSONObject; //换表
//       }
//       this.AddApi(lJSONObject_DataSet);
//     },
//     AddApi(lJSONObject_DataSet) {
//       console.log(lJSONObject_DataSet);
//       const _this = this;
//       $.ajax({
//         url: `${mh}/clientOperation/generalSaveForClient`,
//         method: "post",
//         data: {
//           Data: JSON.stringify(lJSONObject_DataSet),
//         },
//         success: (res) => {
//           console.log(res);
//           // 在普通 JavaScript 中使用 MessageBox
//           ElMessage({
//             message: "提交成功",
//             type: "success",
//             plain: true,
//             duration: 1000, // 消息显示时长（毫秒）
//           });
//           // 等待 3 秒后回退页面
//           setTimeout(() => {
//             window.history.back();
//           }, 1000);
//         },
//         error: (err) => {
//           console.log(err);
//         },
//       });
//     },
//     goBack() {
//       window.history.back();
//     },
//   },
// };



const App = {
  mixins: [Save],//配置文件，路径/utils/mixins.js
  data() {
    return {
      Nation: [],
      Ducation: ducation(),
      city: [],
      cityValue: [],
      Affiliation: [
        {
          value: "群众",
          label: "群众",
        },
        {
          value: "无党派人士",
          label: "无党派人士",
        },
        {
          value: "民主党派成员",
          label: "民主党派成员",
        },
        {
          value: "共青团员",
          label: "共青团员",
        },
        {
          value: "中共预备党员",
          label: "中共预备党员",
        },
        {
          value: "中共党员",
          label: "中共党员",
        },
      ],
    };
  },
  mounted() {
    this.getCategory();
    console.log(this.Ducation);
  },
  watch: {
    'dataForm.ID_Card_Number'(val) {
      console.log(this.dataForm);
      this.cityValue = [this.dataForm.RR_Province_Number, this.dataForm.RR_City_Number, this.dataForm.RR_County_Nubmer, this.dataForm.RR_Town_Number];
      console.log(this.cityValue);
    },

  },
  methods: {
    getCategory() {
      $.ajax({
        url: absolutepath() + "api/Nation.json",
        type: "GET",
        success: (data) => {
          this.Nation = data;
        },
        dataType: "json",
      });
      $.ajax({
        url: `${absolutepath()}api/Districtjl.json`,
        method: "get",
        success: (data) => {
          this.city = data;
        }
      });
    },
    changeCity(val) {
      if (val.length > 0) {
        let Province = this.city.filter(i => i.value == val[0])[0];
        let City = Province.children.filter(i => i.value == val[1])[0];
        let County = City.children.filter(i => i.value == val[2])[0];
        let Town = County.children.filter(i => i.value == val[3])[0];
        this.dataForm.Registered_Residence = Province.label + City.label + County.label + Town.label;
        this.dataForm.RR_Province_Number = val[0];
        this.dataForm.RR_City_Number = val[1];
        this.dataForm.RR_County_Nubmer = val[2];
        this.dataForm.RR_Town_Number = val[3];
      } else {
        this.dataForm.Registered_Residence = '';
        this.dataForm.RR_Province_Number = '';
        this.dataForm.RR_City_Number = '';
        this.dataForm.RR_County_Nubmer = '';
        this.dataForm.RR_Town_Number = '';
      }
      console.log(this.dataForm.Registered_Residence);
    },
    handleChange(file, fileList) {
      const reader = new FileReader();
      reader.readAsDataURL(file.raw);
      reader.onload = () => {
        const base64Data = reader.result;
        console.log("Base64 编码的图片数据：", base64Data);
        const parts = base64Data.split("base64,");
        if (parts.length > 1) {
          var extractedData = parts[1];
          console.log(extractedData); //想要的数据
        } else {
          console.log("未找到 Base64 数据");
        }
        // return
        const _this = this;
        $.ajax({
          headers: {
            "content-type": "application/json",
          },
          url: `${mh}/clientOperation/parseIDCard`,
          method: "post",
          data: extractedData,
          success: (res) => {
            console.log(res);
            console.log(res.words_result[0].card_result);
            _this.personal_information.ID_Card_Number =
              res.words_result[0].card_result.公民身份号码.words;
            _this.personal_information.Personal_Name =
              res.words_result[0].card_result.姓名.words;
            _this.personal_information.Gender =
              res.words_result[0].card_result.性别.words;
            _this.personal_information.Registered_Residence =
              res.words_result[0].card_result.住址.words;
            _this.personal_information.Nation =
              res.words_result[0].card_result.民族.words;
            const birthStr = res.words_result[0].card_result.出生.words;
            if (birthStr.length === 8) {
              const formattedBirthday = `${birthStr.slice(
                0,
                4
              )}-${birthStr.slice(4, 6)}-${birthStr.slice(6)}`;
              _this.personal_information.Birthday = formattedBirthday;
            } else {
              console.log("出生日期格式不正确，应为 8 位数字");
            }
          },
          error: (err) => {
            console.log(err);
          },
        });
      };
      reader.onerror = () => {
        console.error("读取文件时出错");
      };
    },
    async handleSubmit() {
      const allValid = await this.validateAllForms();
      if (allValid) {
        this.loading = true;
        let ls_URL = window.location.href;
        const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
        let lJSONObject_Data_Options = new Object();
        lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

        let ls_MainTableName = lJSONObject_Data_Options.TableName;
        let ls_DataForm = lJSONObject_Data_Options.form;
        let ls_primary_key = lJSONObject_Data_Options.primary_key;
        //没有子表下面内容就会报错，try/catch抛出异常
        try {
          //可能涉及多个子表
          document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
            let lJSONObject_ChildData_Options = new Object();
            lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

            let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
            let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
            // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
            if (this[ls_DataForm][ls_primary_key]) {
              this[ls_ChildDataForm].forEach(item => {
                item[ls_primary_key] = this[ls_DataForm][ls_primary_key];
              })
            }
            //建立子表结构体
            let JSONArray_ChildRecord = new Array();
            JSONArray_ChildRecord = this[ls_ChildDataForm];
            if (!this[ls_DataForm].Children) {
              this[ls_DataForm].Children = new Object();
            }
            this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
          })
        } catch (error) {

        }
        //建立新增提交结构
        let lJSONObject_DataSet = new Object();
        if (action == 'Add') {
          if (this.nodeKey) {
            this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
          }
          let lJSONArray_Record = new Array();
          lJSONArray_Record.push(this[ls_DataForm]);
          let lJSONObject_Table = new Object();
          lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
          lJSONObject_DataSet[action] = lJSONObject_Table;
        } else if (action == 'Modify') {
          const { add, modify } = this.deal(this[ls_DataForm]);
          let lJSONArray_Record = new Array();
          lJSONArray_Record.push(modify);

          let lJSONObject_Table = new Object();
          lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

          lJSONObject_DataSet[action] = lJSONObject_Table;
          if (add != null) {
            let lJSONArray_Record_Add = new Array();
            lJSONArray_Record_Add.push(add);

            let lJSONObject_Table_Add = new Object();
            lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

            lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
          }
        }
        let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
        if (!lJSONObject_DataSet['Add']) {
          lJSONObject_DataSet['Add'] = new Object();
        }
        lJSONObject_DataSet['Add']['base_operation_log'] = [{
          Operation_Type: '0',
          Operation_Path: this.relativePath,
          Operation_Main,
          Operation_Interface: "generalSave",
          Operation_IP: getIpAddress()
        }]
        submitSave(lJSONObject_DataSet, (res) => {
          if (res.Status == 'success') {
            window.location.href = './personal.html'
          }
          this.loading = false;
        })
        // $.ajax({
        //   headers: {
        //     "content-type": "application/x-www-form-urlencoded",
        //   },
        //   url: `${mh}/generalBusiness/generalSave`,
        //   method: "post",
        //   data: {
        //     Data: JSON.stringify(lJSONObject_DataSet),
        //   },
        //   success: (res) => {
        //     if (res.Status == 'success') {
        //       window.location.href = './personal.html'
        //     }
        //   },
        //   complete: () => {
        //     this.loading = false;
        //   }
        // });
      };
    },
  },
};

const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
